package cc.imos.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;

/**
 * Druid 引导配置 <br />
 * Created by asher on 2017/3/6.
 */
@Slf4j
@Configuration
public class BasicDruid {

    @Primary  // 在同样的 DataSource 中，首先使用被标注的 DataSource
    @Bean(initMethod = "init", destroyMethod = "close") // 声明其为 Bean 实例
    @ConfigurationProperties(prefix="spring.datasource")
    public DataSource druidDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        return dataSource;
    }

    @Bean
    public ServletRegistrationBean druidServlet() {
        String urlMapping = "/druid/*";
        String allow = "127.0.0.1";
        String deny = "192.168.1.1";
        String username = "asher";
        String password = "love.asher";
        String reset = "false";

        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), urlMapping);
        //白名单：
        servletRegistrationBean.addInitParameter("allow", allow);
        //IP黑名单 (存在共同时，deny优先于allow) : 如果满足deny的话提示:Sorry, you are not permitted to view this page.
        servletRegistrationBean.addInitParameter("deny", deny);
        //登录查看信息的账号密码.
        servletRegistrationBean.addInitParameter("loginUsername", username);
        servletRegistrationBean.addInitParameter("loginPassword", password);
        //是否能够重置数据.
        servletRegistrationBean.addInitParameter("resetEnable", reset);
        return servletRegistrationBean;
    }


}
